using DocumentCreationSystem.Models.AgentOps;

namespace DocumentCreationSystem.Services.AgentOps
{
    /// <summary>
    /// 智能规划模板执行器接口 - 将静态模板转换为动态工作流
    /// </summary>
    public interface IPlanningTemplateExecutor
    {
        /// <summary>
        /// 解析规划模板
        /// </summary>
        /// <param name="templatePath">模板文件路径</param>
        /// <returns>解析后的规划模板</returns>
        Task<PlanningTemplate> ParseTemplateAsync(string templatePath);

        /// <summary>
        /// 从规划模板创建可执行工作流
        /// </summary>
        /// <param name="template">规划模板</param>
        /// <param name="context">执行上下文</param>
        /// <returns>可执行工作流</returns>
        Task<AgentWorkflow> CreateExecutableWorkflowAsync(PlanningTemplate template, WorkflowCreationContext context);

        /// <summary>
        /// 验证模板完整性
        /// </summary>
        /// <param name="template">规划模板</param>
        /// <returns>验证结果</returns>
        Task<TemplateValidationResult> ValidateTemplateAsync(PlanningTemplate template);

        /// <summary>
        /// 优化模板执行计划
        /// </summary>
        /// <param name="template">规划模板</param>
        /// <param name="optimizationOptions">优化选项</param>
        /// <returns>优化后的模板</returns>
        Task<PlanningTemplate> OptimizeTemplateAsync(PlanningTemplate template, TemplateOptimizationOptions optimizationOptions);

        /// <summary>
        /// 获取模板执行预估
        /// </summary>
        /// <param name="template">规划模板</param>
        /// <returns>执行预估信息</returns>
        Task<TemplateExecutionEstimate> GetExecutionEstimateAsync(PlanningTemplate template);

        /// <summary>
        /// 模板差异分析
        /// </summary>
        /// <param name="template1">模板1</param>
        /// <param name="template2">模板2</param>
        /// <returns>差异分析结果</returns>
        Task<TemplateDiffResult> CompareTemplatesAsync(PlanningTemplate template1, PlanningTemplate template2);

        /// <summary>
        /// 生成模板执行报告
        /// </summary>
        /// <param name="templateId">模板ID</param>
        /// <param name="executionResults">执行结果列表</param>
        /// <returns>执行报告</returns>
        Task<TemplateExecutionReport> GenerateExecutionReportAsync(string templateId, List<WorkflowExecutionResult> executionResults);
    }

    /// <summary>
    /// 规划模板模型
    /// </summary>
    public class PlanningTemplate
    {
        /// <summary>
        /// 模板ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 模板名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 模板描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 模板版本
        /// </summary>
        public string Version { get; set; } = "1.0.0";

        /// <summary>
        /// 模板类型
        /// </summary>
        public TemplateType Type { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 模板内容
        /// </summary>
        public TemplateContent Content { get; set; } = new();

        /// <summary>
        /// 模板元数据
        /// </summary>
        public TemplateMetadata Metadata { get; set; } = new();

        /// <summary>
        /// 模板配置
        /// </summary>
        public TemplateConfiguration Configuration { get; set; } = new();

        /// <summary>
        /// 模板标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 源文件路径
        /// </summary>
        public string? SourceFilePath { get; set; }
    }

    /// <summary>
    /// 模板类型枚举
    /// </summary>
    public enum TemplateType
    {
        /// <summary>
        /// 角色规划模板
        /// </summary>
        CharacterPlanning,

        /// <summary>
        /// 工作流规划模板
        /// </summary>
        WorkflowPlanning,

        /// <summary>
        /// 质量控制模板
        /// </summary>
        QualityControl,

        /// <summary>
        /// 项目管理模板
        /// </summary>
        ProjectManagement,

        /// <summary>
        /// 混合模板
        /// </summary>
        Hybrid
    }

    /// <summary>
    /// 模板内容
    /// </summary>
    public class TemplateContent
    {
        /// <summary>
        /// 原始内容
        /// </summary>
        public string RawContent { get; set; } = string.Empty;

        /// <summary>
        /// 解析后的章节
        /// </summary>
        public List<TemplateSection> Sections { get; set; } = new();

        /// <summary>
        /// 任务列表
        /// </summary>
        public List<TemplateTask> Tasks { get; set; } = new();

        /// <summary>
        /// 变量定义
        /// </summary>
        public Dictionary<string, TemplateVariable> Variables { get; set; } = new();

        /// <summary>
        /// 条件规则
        /// </summary>
        public List<TemplateRule> Rules { get; set; } = new();
    }

    /// <summary>
    /// 模板章节
    /// </summary>
    public class TemplateSection
    {
        /// <summary>
        /// 章节ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 章节标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 章节内容
        /// </summary>
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 章节类型
        /// </summary>
        public SectionType Type { get; set; }

        /// <summary>
        /// 章节级别
        /// </summary>
        public int Level { get; set; } = 1;

        /// <summary>
        /// 父章节ID
        /// </summary>
        public string? ParentId { get; set; }

        /// <summary>
        /// 子章节列表
        /// </summary>
        public List<TemplateSection> Children { get; set; } = new();

        /// <summary>
        /// 章节属性
        /// </summary>
        public Dictionary<string, object> Properties { get; set; } = new();
    }

    /// <summary>
    /// 章节类型枚举
    /// </summary>
    public enum SectionType
    {
        /// <summary>
        /// 标题章节
        /// </summary>
        Header,

        /// <summary>
        /// 内容章节
        /// </summary>
        Content,

        /// <summary>
        /// 任务章节
        /// </summary>
        Task,

        /// <summary>
        /// 配置章节
        /// </summary>
        Configuration,

        /// <summary>
        /// 数据章节
        /// </summary>
        Data,

        /// <summary>
        /// 模板章节
        /// </summary>
        Template
    }

    /// <summary>
    /// 模板任务
    /// </summary>
    public class TemplateTask
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 任务名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 任务描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 任务类型
        /// </summary>
        public TaskType Type { get; set; }

        /// <summary>
        /// 执行顺序
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        /// 依赖任务
        /// </summary>
        public List<string> Dependencies { get; set; } = new();

        /// <summary>
        /// 任务参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();

        /// <summary>
        /// 预估时间（分钟）
        /// </summary>
        public int EstimatedMinutes { get; set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public TaskPriority Priority { get; set; } = TaskPriority.Normal;

        /// <summary>
        /// 是否可选
        /// </summary>
        public bool IsOptional { get; set; } = false;

        /// <summary>
        /// 条件表达式
        /// </summary>
        public string? Condition { get; set; }
    }

    /// <summary>
    /// 任务类型枚举
    /// </summary>
    public enum TaskType
    {
        /// <summary>
        /// 角色创建
        /// </summary>
        CreateCharacter,

        /// <summary>
        /// 内容生成
        /// </summary>
        GenerateContent,

        /// <summary>
        /// 文件创建
        /// </summary>
        CreateFile,

        /// <summary>
        /// 质量检查
        /// </summary>
        QualityCheck,

        /// <summary>
        /// 数据处理
        /// </summary>
        ProcessData,

        /// <summary>
        /// 用户输入
        /// </summary>
        UserInput,

        /// <summary>
        /// 条件判断
        /// </summary>
        Condition,

        /// <summary>
        /// 循环执行
        /// </summary>
        Loop,

        /// <summary>
        /// 自定义任务
        /// </summary>
        Custom
    }

    /// <summary>
    /// 任务优先级枚举
    /// </summary>
    public enum TaskPriority
    {
        Low = 1,
        Normal = 2,
        High = 3,
        Critical = 4
    }

    /// <summary>
    /// 模板变量
    /// </summary>
    public class TemplateVariable
    {
        /// <summary>
        /// 变量名
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 变量类型
        /// </summary>
        public VariableType Type { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public object? DefaultValue { get; set; }

        /// <summary>
        /// 是否必需
        /// </summary>
        public bool IsRequired { get; set; } = false;

        /// <summary>
        /// 变量描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 验证规则
        /// </summary>
        public List<ValidationRule> ValidationRules { get; set; } = new();
    }

    /// <summary>
    /// 变量类型枚举
    /// </summary>
    public enum VariableType
    {
        String,
        Integer,
        Boolean,
        DateTime,
        Array,
        Object
    }

    /// <summary>
    /// 验证规则
    /// </summary>
    public class ValidationRule
    {
        /// <summary>
        /// 规则类型
        /// </summary>
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 规则参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;
    }

    /// <summary>
    /// 模板规则
    /// </summary>
    public class TemplateRule
    {
        /// <summary>
        /// 规则ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 规则名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 条件表达式
        /// </summary>
        public string Condition { get; set; } = string.Empty;

        /// <summary>
        /// 动作列表
        /// </summary>
        public List<RuleAction> Actions { get; set; } = new();

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;
    }

    /// <summary>
    /// 规则动作
    /// </summary>
    public class RuleAction
    {
        /// <summary>
        /// 动作类型
        /// </summary>
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 动作参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();
    }

    /// <summary>
    /// 模板验证结果
    /// </summary>
    public class TemplateValidationResult
    {
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid { get; set; } = true;

        /// <summary>
        /// 错误列表
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 警告列表
        /// </summary>
        public List<string> Warnings { get; set; } = new();

        /// <summary>
        /// 验证时间
        /// </summary>
        public DateTime ValidationTime { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 模板优化选项
    /// </summary>
    public class TemplateOptimizationOptions
    {
        /// <summary>
        /// 是否优化性能
        /// </summary>
        public bool OptimizeForPerformance { get; set; } = true;

        /// <summary>
        /// 是否优化质量
        /// </summary>
        public bool OptimizeForQuality { get; set; } = true;

        /// <summary>
        /// 是否启用并行化
        /// </summary>
        public bool EnableParallelization { get; set; } = false;

        /// <summary>
        /// 最大并行度
        /// </summary>
        public int MaxParallelism { get; set; } = 4;
    }

    /// <summary>
    /// 模板执行预估
    /// </summary>
    public class TemplateExecutionEstimate
    {
        /// <summary>
        /// 模板ID
        /// </summary>
        public string TemplateId { get; set; } = string.Empty;

        /// <summary>
        /// 模板名称
        /// </summary>
        public string TemplateName { get; set; } = string.Empty;

        /// <summary>
        /// 预估执行时间（分钟）
        /// </summary>
        public int EstimatedDurationMinutes { get; set; }

        /// <summary>
        /// 资源需求
        /// </summary>
        public ResourceRequirements ResourceRequirements { get; set; } = new();

        /// <summary>
        /// 复杂度评分 (0-1)
        /// </summary>
        public double ComplexityScore { get; set; }

        /// <summary>
        /// 风险因素
        /// </summary>
        public List<string> RiskFactors { get; set; } = new();

        /// <summary>
        /// 执行建议
        /// </summary>
        public List<string> ExecutionRecommendations { get; set; } = new();
    }

    /// <summary>
    /// 资源需求
    /// </summary>
    public class ResourceRequirements
    {
        /// <summary>
        /// 是否CPU密集型
        /// </summary>
        public bool CpuIntensive { get; set; }

        /// <summary>
        /// 是否内存密集型
        /// </summary>
        public bool MemoryIntensive { get; set; }

        /// <summary>
        /// 磁盘空间需求（字节）
        /// </summary>
        public long DiskSpaceRequired { get; set; }

        /// <summary>
        /// 是否需要网络连接
        /// </summary>
        public bool NetworkRequired { get; set; }
    }

    /// <summary>
    /// 模板差异结果
    /// </summary>
    public class TemplateDiffResult
    {
        /// <summary>
        /// 差异摘要
        /// </summary>
        public string Summary { get; set; } = string.Empty;

        /// <summary>
        /// 添加的项目
        /// </summary>
        public List<string> AddedItems { get; set; } = new();

        /// <summary>
        /// 删除的项目
        /// </summary>
        public List<string> RemovedItems { get; set; } = new();

        /// <summary>
        /// 修改的项目
        /// </summary>
        public List<string> ModifiedItems { get; set; } = new();
    }

    /// <summary>
    /// 模板执行报告
    /// </summary>
    public class TemplateExecutionReport
    {
        /// <summary>
        /// 模板ID
        /// </summary>
        public string TemplateId { get; set; } = string.Empty;

        /// <summary>
        /// 执行次数
        /// </summary>
        public int ExecutionCount { get; set; }

        /// <summary>
        /// 成功次数
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败次数
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 平均执行时间
        /// </summary>
        public TimeSpan AverageExecutionTime { get; set; }

        /// <summary>
        /// 性能趋势
        /// </summary>
        public List<PerformanceDataPoint> PerformanceTrend { get; set; } = new();
    }

    /// <summary>
    /// 性能数据点
    /// </summary>
    public class PerformanceDataPoint
    {
        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// 执行时间（毫秒）
        /// </summary>
        public long ExecutionTimeMs { get; set; }

        /// <summary>
        /// 成功状态
        /// </summary>
        public bool Success { get; set; }
    }
}
